<template>
  <div class='container'>
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float:right"
          type="primary"
          size="small"
          @click="search_button()">
          查询搜索
        </el-button>
        <el-button
          style="float:right;margin-right: 15px"
          size="small"
          @click="reset_button()">
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true"  size="small" label-width="140px">
          <el-form-item label="输入搜索：">
            <el-input  class="input-width" v-model="keyword_value" placeholder="商品" clearable></el-input>
          </el-form-item>
          <el-form-item label="商品类型：">
            <el-select placeholder="请输入商品类型" v-model="chosen_category_option" @change="category_changing">
              <el-option
                v-for="item in category_options"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>商品列表</span>
      <el-button size="mini" class="btn-add" @click="multiple_delete_button()" style="margin-left: 20px; float:right" icon='el-icon-delete' type="danger">批量删除</el-button>
      <el-button size="mini" class="btn-add" @click="add_button()" style="margin-left: 20px; float:right" icon='el-icon-plus' type="primary">添加</el-button>
    </el-card>
    <div class='table-container'>
      <el-table
        class='m-30-t'
        :data="goods_list"
        fit
        stripe
        border
        @select="chosen_option"
        @select-all="all_options"
        v-loading="listLoading"
        highlight-current-row>
        <el-table-column type="selection" ></el-table-column>
        <el-table-column
          prop="id"
          label="编号"
          align="left"
          width="100">
        </el-table-column>
        <el-table-column
          prop="name"
          label="商品名称"
          align="center">
        </el-table-column>
        <el-table-column
          prop="point"
          label="积分"
          align="center">
        </el-table-column>
        <el-table-column
          prop="cost_price"
          label="成本价"
          align="center">
        </el-table-column>
        <el-table-column
          prop="img"
          label="商品图片"
          align="center">
          <template slot-scope="scope">
            <el-image :src="scope.row.img" class='good-img' v-if="scope.row.img"></el-image>
            <span v-else>无</span>
          </template>
        </el-table-column>
        <el-table-column
          label="类型"
          align="center">
          <template slot-scope="scope">
            <span>{{scope.row.type=='costpoint'?'积分商品':'一般商品'}}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column
          prop="goods_no"
          label="商品货号"
          align="center">
        </el-table-column> -->
        <el-table-column
          label="状态"
          align="center"
          width="150px">
          <template slot-scope="scope">
            <div class="">
              <span v-if="scope.row.is_del==0">正常</span>
              <span v-if="scope.row.is_del==2">已下架</span>
              <!-- <el-switch
                v-model="scope.row.is_del"
                :active-value="1"
                :inactive-value="0"
                inactive-text="上架"
                @change="del_change($event,scope.row.id)">
              </el-switch> -->
              <!-- <el-switch
                v-model="scope.row.is_share"
                :active-value="1"
                :inactive-value='0'
                inactive-text="推荐"
                class="">
              </el-switch> -->
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="store_nums"
          label="库存"
          align="center"
          width="100px">
        </el-table-column>
        <el-table-column
          prop="is_share"
          label="首页显示"
          align="center"
          width="100px">
          <template slot-scope="scope">
            <div class="">
              <span v-if="scope.row.is_share==0">不显示</span>
              <span v-if="scope.row.is_share==1">显示</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="create_time"
          label="上架时间"
          align="center"
          width="100px">
        </el-table-column>
        <el-table-column
          prop="sell_price"
          label="销售价格"
          align="center"
          width="100px">

        </el-table-column>
        <el-table-column
          prop="sort"
          label="排序"
          align="center"
          width="100px">
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="300px"
          fixed="right">
          <template slot-scope="scope">
            <el-button  size="small" type="warning" @click="detail_button(scope.row.id)">详情</el-button>
            <el-button  size="small" type="danger" @click="delete_button(scope.row.id)">删除</el-button>
            <el-button  size="small" type="success" @click="edit_button(scope.$index, scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container m-50-t m-50-b">
      <span></span>
      <el-pagination
        background
        @size-change="size_change"
        @current-change="current_change"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="page_num"
        :page-size="page_size"
        :page-sizes="page_sizes"
        :total="total">
      </el-pagination>
    </div>
    <el-dialog
      title="商品详情"
      width="50%"
      :visible.sync="detail_dialog_visible">
      <div>
        <span class="title">商品名称：</span>
        <span class="m-10-l ">{{detail_data.name}}</span>
      </div>
      <div class=' m-30-t'>
        <span class="title">商品编号：</span>
        <span class="m-10-l  ">{{detail_data.goods_no}}</span>
      </div>
      <div class=' m-30-t'>
        <span class="title">创建日期：</span>
        <span class="m-10-l  ">{{detail_data.create_time}}</span>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="detail_dialog_visible = false" size="small">取 消</el-button>
        <el-button @click="detail_dialog_visible = false" type="primary" size="small">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import goods_category_request from "@/api/goods_category_request"
import goods_request from "@/api/goods_request"
export default {
  name: 'goods',
  data() {
    return {
      base: 'http://pai.xtyxmall.com/data/',
      delete_list: [],
      detail_data: {},
      goods_list: [],
      dialog_visible: false,
      isEdit: false,
      search_value: '',
      page_num: 1,
      page_size: 10,
      page_sizes: [5,10,15],
      total: null,
      name: '',
      goods_no: '',
      birthday: '',
      create_time: '',
      detail_dialog_visible: false,
      listLoading: false,
      keyword: '',
      keyword_value: '',
      current_size: 10,
      current_page: 1,
      target_id: '',
      category_options: [],
      goods_category_list: [],
      chosen_category_option: '',
      type_value: '',
    }
  },
  created() {
    this.get_list()
    this.get_category_list()
  },
  mounted() {

  },
  methods: {
    chosen_option(id_list) {
      this.delete_list = id_list
    },
    del_change(val,id) {
      let params = {
        is_del: val,
        id
      }
      goods_request.edit_goods(params, res=> {

      })
      console.log(val,b);
    },
    get_category_list() {
      let params = {
        pageSize: 100, page: 1
      }
      goods_category_request.goods_category_list(params, res=>{
        this.category_options = res.data
      })
    },
    category_changing(e) {
      this.type_value = e
    },
    all_options(id_list) {
      this.delete_list = id_list
    },
    multiple_delete_button() {
      this.$confirm('是否要进行批量删除操作?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        for (let item of this.delete_list) {
          goods_request.delete_goods({id: item.id}, res=> {
            this.get_list()
          })
        }
        this.delete_list = []
      }).catch(err=>err)
    },
    // 搜索
    search_button() {
      this.current_page = 1
      this.keyword = this.keyword_value
      this.get_list()
    },
    // 重置
    reset_button() {
      this.keyword = ''
      this.keyword_value= ''
      this.get_list()
    },
    // 添加
    add_button() {
      this.$router.push({path:'/academic/goods_add'});
    },
    // 确认
    // 删除
    delete_button(id) {
      this.$confirm('是否要进行删除操作?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = {
          id
        }
        goods_request.delete_goods(params, res=> {
          this.get_list()
        })
      }).catch(err=>err)
    },
    // 编辑
    edit_button(index, item) {
      // this.dialog_visible = true;
      // this.isEdit = true;
      // this.name = item.name
      // this.create_time = item.create_time
      // this.goods_no = item.goods_no
      // this.birthday = item.birthday
      // this.target_id = item.id
      this.$router.push({path: '/academic/goods_edit', query:{id: item.id, gid: item.goods_no}})
    },
    // 详情
    detail_button(id) {
      goods_request.detail_goods({id}, res=> {
        this.detail_data = res.data
      })
      this.detail_dialog_visible = true
      // this.$router.push({path: '/academic/goods_detail', query:{id: item.id}})

    },
    size_change(current_size) {
      this.current_size = current_size
      this.get_list()
    },
    current_change(current_page) {
      this.current_page = current_page
      this.get_list()
    },
    // filter_list() {
    //   goods_request.goods_list({pageSize: this.current_size, key: this.keyword, page: this.search_page}, res=> {
    //     this.goods_list = res.data
    //     this.total = res.data.length
    //   })
    // },
    get_list() {
      this.listLoading = true;
      goods_request.goods_list({pageSize: this.current_size, name: this.keyword, page: this.current_page,type: this.type_value}, res=> {
        this.listLoading = false
        for(const item of res.data) {
          let a = ''
          const temp = 'http://img.gangjinhui.cn'
          const b = item.img.substring(0, 20)
          // test
          if (b === 'http://img.gzzsu.com') {
            a = item.img.substring(20, item.img.length)
            item.img = temp + a
          }
        }
        this.goods_list = res.data
        this.total = res.totalRecord
      })
    },
  }
}
</script>

<style scoped>
@import "~@/styles/layout.scss";
.table-container {
  margin-left: 30px;
}
.pagination-container {
  float: right;
}

.table-item {
  height: 100px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.good-img {
  width: 100px;
  height:100px;
}
</style>
